import { defineStore } from "pinia";
import { reactive } from "vue";
import type { KeepAliveNamesState} from './types/index'

export const useKeepAliveNames = defineStore("keepAliveNames", ()=>{
    const state = reactive<KeepAliveNamesState>({
        keepAliveNames: [],
		cachedViews: [],
    })
    async function setCacheKeepAlive(data: Array<string>) {
        state.keepAliveNames = data;
    }
    async function addCachedView(view: any) {
        if (view.meta.isKeepAlive) state.cachedViews?.push(view.name);
    }
    async function delCachedView(view: any) {
        const index = state.cachedViews.indexOf(view.name);
        setTimeout(() => {
            index > -1 && state.cachedViews.splice(index, 1);
        }, 20);
    }
    async function delOthersCachedViews(view: any) {
        if (view.meta.isKeepAlive) state.cachedViews = [view.name];
        else state.cachedViews = [];
    }
    async function delAllCachedViews() {
        state.cachedViews = [];
    }
    return {
        state,
        setCacheKeepAlive,
        addCachedView,
        delCachedView,
        delOthersCachedViews,
        delAllCachedViews,
    }
})